﻿using Commons.Requests;
using Commons.Responses;
using DomainCommons.Models;
using IApplicationServices;
using IRepositories;
using Microsoft.AspNetCore.Mvc;
using WebAPI.Fileters;

namespace WebAPI.Controllers
{
    [TypeFilter(typeof(InitFilter))]
    [ApiController]
    public class BaseController<T, TEntity, TApiEntity> : ControllerBase
        where TEntity : class, IEntity<T>, new()
        where TApiEntity : class, IEntity<T>, new()
    {
        private readonly IBaseApplicationService<T, TEntity, TApiEntity> _apiService;
        private readonly IBaseRepository<T, TEntity> _repository;

        protected IBaseApplicationService<T, TEntity, TApiEntity> ApiService { get => _apiService; }
        protected IBaseRepository<T, TEntity> Repository { get => _repository; }

        public string AppUserId { get; set; }


        public BaseController(IBaseApplicationService<T, TEntity, TApiEntity> apiService)
        {
            _apiService = apiService;
            _repository = apiService.Repository;
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public virtual async Task<DatasResponse<TApiEntity>> GetAsync() => await _apiService.GetAllAsync();
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="apiEntity"></param>
        /// <returns></returns>
        [HttpPost]
        public virtual async Task<MessageResponse> AddAsync(TApiEntity apiEntity) => await _apiService.AddAsync(apiEntity);

        /// <summary>
        /// 根据id查询一条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        
        [HttpGet]
        public virtual async Task<DataResponse<TApiEntity>> GetByIdAsync(T id)=>await _apiService.GetByIdAsync(id);

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="apiEntity"></param>
        /// <returns></returns>
        [HttpPost]
        public virtual async Task<MessageResponse> UpdateAsync(TApiEntity apiEntity) => await _apiService.UpdateAsync(apiEntity);
        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public virtual async Task<MessageResponse> SoftDeleteByIdAsync(T id)
        {

            return await _apiService.SoftDeleteByIdAsync(id);
        }
        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public virtual async Task<MessageResponse> SoftDeleteRangeAsync(List<T> ids)
        {
            return await _apiService.SoftDeleteRangeByIdAsync(ids);
        }
        /// <summary>
        /// 根据条件进行查询
        /// </summary>
        /// <param name="selectParameter"></param>
        /// <returns></returns>
        [HttpGet]
        public virtual async Task<DatasResponse<TApiEntity>> GetBySelectAsync([FromQuery] PagingRequest pagingRequest)
        {
            if (!string.IsNullOrEmpty(pagingRequest.SearchValue))
            {
                return await _apiService.GetBySelectAsync(pagingRequest.PageIndex, pagingRequest.PageSize, x => x.Name.Contains(pagingRequest.SearchValue));
            }
            return await _apiService.GetBySelectAsync(pagingRequest.PageIndex, pagingRequest.PageSize, null);
        }




    }
}
